var deviceTree;
		
function initTree() {
	var menu = new dhtmlXMenuObject();
	menu.setIconsPath(contextPath + '/resources/common/js/dhtmlx_std_full/imgs/');
	menu.renderAsContextMenu();
	menu.addNewChild(menu.topId, 0, '장비 추가', '장비 추가', false, '');
	menu.addNewChild(menu.topId, 1, '장비 수정', '장비 수정', false, '');
	menu.addNewChild(menu.topId, 2, '장비 삭제', '장비 삭제', false, '');
	
	menu.attachEvent('onClick',function (menuitemId,type){
		switch (menuitemId) {
		case '장비 추가' :
			add();
			break;
		case '장비 수정' :
			update();
			break;
		case '장비 삭제' :
			del();
			break;
		}
	});
	
	deviceTree = tab_device.attachTree();
	deviceTree.setIconsPath(contextPath + '/resources/common/js/dhtmlx_std_full/imgs/', function(){});
	deviceTree.enableTreeLines(true);
	deviceTree.enableTreeImages(false);
	deviceTree.enableContextMenu(menu);
	deviceTree.insertNewChild(0, 'root', 'NETWORK');
	
	var tableRequest = $.ajax({
		url : '/bwf/device/',
		type : 'GET',
		dataType : 'json'
	});
	tableRequest.done(function(info) {
		for ( var i = 0; i < info.length; i++ ) {
			data = info[i];
			dataStore.put(data['id'], data);
			deviceTree.insertNewChild('root', data['id'], data['name']);
		}
	});
	tableRequest.fail(function(jqXHR, textStatus) {
		alert('Request failed!');
	});
	
	deviceTree.attachEvent('onSelect', selectedTree);
	
	deviceTree.attachEvent('onBeforeContextMenu',function(itemId){
		if ( itemId == 'root' ) {
			menu.setItemEnabled('장비 추가');
			menu.setItemDisabled('장비 수정');
			menu.setItemDisabled('장비 삭제');
		}
		
		else {
			menu.setItemEnabled('장비 수정');
			menu.setItemEnabled('장비 삭제');
			menu.setItemDisabled('장비 추가');
		}
		return true;
    });
}

function selectedTree(id) {
	console.log('tree selected id : ' + id);
	if ( id != 'root' ) {
		product  = deviceTree.getItemText(id);
		seNum	 = id;  
		actiCode = '@12ab' + id;
		
		createMarker(id);
		createChannelTable(id);
		createRequestLog();
		setInfoFormValue(product, seNum, actiCode);	
	}
}

function add() {
	var id 	 = deviceTree.getSelectedItemId();
	var text = deviceTree.getSelectedItemText();
	console.log(id);
	console.log(text);
	
	var dhxWins= new dhtmlXWindows();

	var deviceAddWindow = dhxWins.createWindow('deviceAddWin', 100, 100, 300, 300);
	//chartWin.attachLayout('1C');
	deviceAddWindow.setText('장비 등록');
	
	deviceAdd_formData = [ { type:'settings', labelWidth:80, inputWidth:250, position:'absolute'  },
		              	  { type:'label', name:'label_name', label:'Name', width:75, labelWidth:75, labelAlign:'left', labelLeft:20, labelTop:8  },
		              	  { type:'input', name:'input_name', inputWidth:120, inputLeft:120, inputTop:10 },
		            	  { type:'label', name:'label_latitude', label:'Latitude', width:75, labelWidth:75, labelAlign:'left', labelLeft:20, labelTop:58  },
		            	  { type:'input', name:'input_latitude', inputWidth:120, inputLeft:120, inputTop:60 },
		            	  { type:'label', name:'label_longitude', label:'Longitude', width:75, labelWidth:75, labelAlign:'left', labelLeft:20, labelTop:108  },
		            	  { type:'input', name:'input_longitude', inputWidth:120, inputLeft:120, inputTop:110 },
		            	  { type:'button', name:'addButton', label:'등록', value:'등록', inputWidth:100, inputLeft:200, inputTop:200  } ];

	var deviceAdd_form = deviceAddWindow.attachForm(deviceAdd_formData);
	
	deviceAdd_form.attachEvent('onButtonClick', function(name){
		if ( name == 'addButton') {
			var deviceName 		= deviceAdd_form.getItemValue('input_name');
			var deviceLatitude 	= deviceAdd_form.getItemValue('input_latitude');
			var deviceLongitude = deviceAdd_form.getItemValue('input_longitude');
			var json = $.toJSON({ "id" : 0, "name" : deviceName, "latitude" : deviceLatitude, "longitude" : deviceLongitude });

			var deviceAddRequest = $.ajax({
				url : '/bwf/device/'
				,type : 'POST'
				,contentType : 'application/json; charset=UTF-8'
				,data : json
				,dataType : 'json'
			});
			deviceAddRequest.done(function(data) {
				dhxWins.window('deviceAddWin').close();
				initTree();
			});
			deviceAddRequest.fail(function(jqXHR, textStatus) {
				alert('Request failed: ' + textStatus);
			});
		}
	});
}


function update() {
	var id 	 = deviceTree.getSelectedItemId();
	console.log(id);
	
	var dhxWins= new dhtmlXWindows();

	var deviceUpdateWindow = dhxWins.createWindow('deviceUpdateWin', 100, 100, 300, 300);
	//chartWin.attachLayout('1C');
	deviceUpdateWindow.setText('장비 수정');
	
	deviceUpdate_formData = [ { type:'settings', labelWidth:80, inputWidth:250, position:'absolute'  },
		              	  { type:'label', name:'label_name', label:'Name', width:75, labelWidth:75, labelAlign:'left', labelLeft:20, labelTop:8  },
		              	  { type:'input', name:'input_name', inputWidth:120, inputLeft:120, inputTop:10 },
		            	  { type:'label', name:'label_latitude', label:'Latitude', width:75, labelWidth:75, labelAlign:'left', labelLeft:20, labelTop:58  },
		            	  { type:'input', name:'input_latitude', inputWidth:120, inputLeft:120, inputTop:60 },
		            	  { type:'label', name:'label_longitude', label:'Longitude', width:75, labelWidth:75, labelAlign:'left', labelLeft:20, labelTop:108  },
		            	  { type:'input', name:'input_longitude', inputWidth:120, inputLeft:120, inputTop:110 },
		            	  { type:'button', name:'updateButton', label:'수정', value:'수정', inputWidth:100, inputLeft:200, inputTop:200  } ];

	var deviceUpdate_form = deviceUpdateWindow.attachForm(deviceUpdate_formData);
	
	var oneDataInfo = dataStore.get(id);
	deviceUpdate_form.setItemValue('input_name', oneDataInfo['name']);
	deviceUpdate_form.setItemValue('input_latitude', oneDataInfo['latitude']);
	deviceUpdate_form.setItemValue('input_longitude', oneDataInfo['longitude']);
	
	deviceUpdate_form.attachEvent('onButtonClick', function(name){
		if ( name == 'updateButton') {
			var deviceName 		= deviceUpdate_form.getItemValue('input_name');
			var deviceLatitude 	= deviceUpdate_form.getItemValue('input_latitude');
			var deviceLongitude = deviceUpdate_form.getItemValue('input_longitude');
			var json = $.toJSON({ "id" : id, "name" : deviceName, "latitude" : deviceLatitude, "longitude" : deviceLongitude });

			var deviceUpdateRequest = $.ajax({
				url : '/bwf/device/'
				,type : 'PUT'
				,contentType : 'application/json; charset=UTF-8'
				,data : json
				,dataType : 'json'
			});
			deviceUpdateRequest.done(function(data) {
				dhxWins.window('deviceUpdateWin').close();
				initTree();
			});
			deviceUpdateRequest.fail(function(jqXHR, textStatus) {
				alert('Request failed: ' + textStatus);
			});
		}
	});
}

function del() {
	var id 	 = deviceTree.getSelectedItemId();
	console.log(id);
	
	var json = $.toJSON({ "id" : id });
	var deviceDeleteRequest = $.ajax({
		url : '/bwf/device/' + id
		,type : 'DELETE'
		,contentType : 'application/json; charset=UTF-8'
		,data : json
		,dataType : 'json'
	});
	deviceDeleteRequest.done(function(data) {
		initTree();
	});
	deviceDeleteRequest.fail(function(jqXHR, textStatus) {
		alert('Request failed: ' + textStatus);
	});
}